package org.ing.sisval.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.ing.sisval.be.BETerreno;
import org.rx.cr.dao.DAOAbstract;

public class DATerreno extends DAOAbstract<BETerreno> {

    public DATerreno(Connection conn) {
        setConnection(conn);
    }

    @Override
    public int registrar(BETerreno be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_ins_terreno");
            setParameterString(be.getTipolote());
            setParameterString(be.getCaracteristicalote());
            setParameterDouble(be.getAncho());
            setParameterDouble(be.getLargo());
            setParameterDouble(be.getArea());
            setParameterDouble(be.getValorunitario());
            setParameterInt(be.getNumlados());
            setParameterDouble(be.getAnchopasadizo());
            setParameterDouble(be.getDistanciapasadizo());
            setParameterDouble(be.getAnchodominio());
            setParameterDouble(be.getLargodominio());
            setParameterDouble(be.getCoeficiente());
             setParameterDouble(be.getLargopasaje());
            setParameterDouble(be.getAnchopasaje());           
            setParameterDouble(be.getValortotal());
            setParameterString(be.getTipovaluacion());
            setParameterDouble(be.getPorcentajeDerecho());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BETerreno be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_upd_terreno");
            setParameterInt(be.getTerrenoid());
            setParameterString(be.getTipolote());
            setParameterString(be.getCaracteristicalote());
            setParameterDouble(be.getAncho());
            setParameterDouble(be.getLargo());
            setParameterDouble(be.getArea());
            setParameterDouble(be.getValorunitario());
            setParameterInt(be.getNumlados());
            setParameterDouble(be.getAnchopasadizo());
            setParameterDouble(be.getDistanciapasadizo());
            setParameterDouble(be.getAnchodominio());
            setParameterDouble(be.getLargodominio());
            setParameterDouble(be.getCoeficiente());
            setParameterDouble(be.getAnchopasaje());
            setParameterDouble(be.getLargopasaje());
            setParameterDouble(be.getValortotal());
            setParameterString(be.getTipovaluacion());
            setParameterDouble(be.getPorcentajeDerecho());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BETerreno be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_del_terreno");
            setParameterInt(be.getTerrenoid());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BETerreno> buscarReferencia(String ref) throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_terreno");
            setParameterString(ref);
            commitc();
            while (existResult()) {
                BETerreno be = new BETerreno();
                be.setTerrenoid(getDataInt(1));
                be.setTipolote(getDataString(2));
                be.setCaracteristicalote(getDataString(3));
                be.setAncho(getDataDouble(4));
                be.setLargo(getDataInt(5));
                be.setArea(getDataInt(6));
                be.setValorunitario(getDataInt(7));
                be.setNumlados(getDataInt(8));
                be.setAnchopasadizo(getDataDouble(9));
                be.setDistanciapasadizo(getDataInt(10));
                be.setAnchodominio(getDataDouble(11));
                be.setLargodominio(getDataDouble(12));
                be.setCoeficiente(getDataInt(13));
                be.setLargopasaje(getDataDouble(14));
                be.setAnchopasaje(getDataDouble(15));
                be.setValortotal(getDataDouble(16));
                be.setTipovaluacion(getDataString(17));
                be.setPorcentajeDerecho(getDataDouble(18));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BETerreno> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_terreno");
            setParameterString("");
            commitc();
            while (existResult()) {
                BETerreno be = new BETerreno();
                be.setTerrenoid(getDataInt(1));
                be.setTipolote(getDataString(2));
                be.setCaracteristicalote(getDataString(3));
                be.setAncho(getDataDouble(4));
                be.setLargo(getDataInt(5));
                be.setArea(getDataInt(6));
                be.setValorunitario(getDataInt(7));
                be.setNumlados(getDataInt(8));
                be.setAnchopasadizo(getDataDouble(9));
                be.setDistanciapasadizo(getDataInt(10));
                be.setAnchodominio(getDataDouble(11));
                be.setLargodominio(getDataDouble(12));
                be.setCoeficiente(getDataInt(13));
                be.setLargopasaje(getDataDouble(14));
                be.setAnchopasaje(getDataDouble(15));
                be.setValortotal(getDataDouble(16));
                be.setTipovaluacion(getDataString(17));
                be.setPorcentajeDerecho(getDataDouble(18));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BETerreno buscar(int i) throws Exception {
        try {
            BETerreno be = null;

            setStoreProcedure("SISVAL.sp_sel_terreno_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BETerreno();
                be.setTerrenoid(getDataInt(1));
                be.setTipolote(getDataString(2));
                be.setCaracteristicalote(getDataString(3));
                be.setAncho(getDataDouble(4));
                be.setLargo(getDataInt(5));
                be.setArea(getDataInt(6));
                be.setValorunitario(getDataInt(7));
                be.setNumlados(getDataInt(8));
                be.setAnchopasadizo(getDataDouble(9));
                be.setDistanciapasadizo(getDataInt(10));
                be.setAnchodominio(getDataDouble(11));
                be.setLargodominio(getDataDouble(12));
                be.setCoeficiente(getDataInt(13));
                be.setLargopasaje(getDataDouble(14));
                be.setAnchopasaje(getDataDouble(15));
                be.setValortotal(getDataDouble(16));
                be.setTipovaluacion(getDataString(17));
                be.setPorcentajeDerecho(getDataDouble(18));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    public int ObtenerUltimoId() throws Exception {
        BETerreno be = new BETerreno();
        try {
            setStoreProcedure("sp_obteneridterreno");
            while (existResult()) {
                be.setTerrenoid(getDataInt(1));
            }

            return Integer.parseInt(commit().toString());

        } catch (Exception e) {
            return rollback(e);
        }

    }

}
